//
// Created by yewei on 2023/1/4.
//
#include <bits/stdc++.h>

using namespace std;

//模拟A除以B，并返回余数R，A = B*Q + R
int cal(string A,int B){
//    int a = A[0]-'0';
    int now=0;
    for(int i=0;i<A.length();i++){
        now = now*10 + A[i]-'0';
        if(now>=B) now%=B;
    }
    return now;
}

int main() {
    freopen("../input.txt", "r", stdin);
    int T;
    cin>>T;
    string n;
    int m;
    while (T--){
        cin>>n>>m;
        cout<<cal(n,m)<<endl;
    }
}

#include <iostream>
#include <vector>
#include <sstream>

using namespace std;

int main(){
    string A;
    int B;
    cin>>A>>B;
    vector<int> Q;
    int R=0;

    int a = A[0]-'0';

    for(int i=0;i<A.length();i++){
        if(a<B){
            if(i == A.length()-1){
                Q.push_back(0);
                R = a;
                break;
            }
            if(i!=0)  Q.push_back(0);
            int a1 = A[i+1]-'0';
            a = a*10+a1;
        } else {
            if(i == A.length()-1){
                Q.push_back(a/B);
                R = a%B;
                break;
            }
            Q.push_back(a/B);
            a = a%B;
            int a1 = A[i+1]-'0';
            a = a*10+a1;
        }
    }

    if(Q.size()==0) cout<<0;
    for(int i=0;i<Q.size();i++){
        cout<<Q[i];
    }
    cout<<" "<<R<<endl;

    return 0;
}

#include <iostream>
#include <vector>
#include <sstream>

using namespace std;

int main(){
    string A;
    int B;
    cin>>A>>B;
    vector<int> Q;
    int R=0;

    for(int i=0;i<A.length();i++){
        R = R*10 + A[i]-'0';
        if(R>B){
            Q.push_back(R/B);
            R %= B;
        }
    }

    if(Q.size()==0) cout<<0;
    int flag = 0;
    for(int i=0;i<Q.size();i++){
        if(Q[i]) flag=1;
        if(flag) cout<<Q[i];
        if(!flag && Q[i]==0) continue;
    }
    cout<<" "<<R<<endl;

    return 0;
}
